﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using WindowsGame1.gamePlugins.renderer;
using WindowsGame1;
using Cookie_Hero.graphics;
using Microsoft.Xna.Framework.Graphics;

namespace Cookie_Hero.physics
{
    public class Collisions
    {

        /* Detecteer collision met de buitenranden en weerkaats het object
         * @param viewPort: width en height van de scene en dus de muren
         * @RETURN: het weerkaatste object OF null
         */
        public static Rectangle wallCollision(PhysicsObject obj, ImageSequenceController animations, GameDisplay.Viewport viewPort)
        {
            Rectangle crash = new Rectangle(0, 0, 0, 0);
            Vector2 min = Vector2.Zero;
            Vector2 max = Vector2.One;
            // TODO neem origin en scale mee in deze berekening
            if (obj.aniTexture != 0)
            {
                try
                {
                    Texture2D texture = animations.getSequenceImage(obj.aniTexture);
                    max = new Vector2(viewPort.width - texture.Width * obj.scale, viewPort.height - texture.Height * obj.scale);
                }
                catch (Exception e)
                {
                    Log.W("Size texture "+obj.name+" undefined");
                }
            }

            if (obj.position.X > max.X)
            {
                crash.X = 1;
            }
            else if (obj.position.X < min.X)
            {
                crash.Width = 1;
            }

            if (obj.position.Y > max.Y)
            {
                crash.Y = 1;
            }
            else if (obj.position.Y < min.Y)
            {
                crash.Height = 1;
            }

            // Er is een botsing met de muren
            return crash;
        }

        /* square collision checker */
        public static bool collision(Rectangle objColl, Rectangle obj2)
        {
            return objColl.Intersects(obj2);
        }

        /* Detecteer collision met andere objecten en weerkaats het object
         * @param viewPort: width en height van de scene en dus de muren
         * @param objectsx: de lijst van alle objecten
         * @RETURN: het weerkaatste object OF null
         */
        public static List<String> collisionRenderer(PhysicsObject obj, ImageSequenceController animations, List<PhysicsObject> objects)
        {
            List<String> returnValue = new List<String>();
            if (!PhysicsOperation.isVisual(obj))
            {
                return returnValue;
            }

            Texture2D texture = getAniTexture(obj, animations);
            // new Rectangle ( X, Y, Width, Height )
            Rectangle Robj2 = new Rectangle();

            if (texture != null)
            {
                Rectangle Robj = new Rectangle((int)obj.position.X, (int)obj.position.Y, (int)(texture.Width * obj.scale), (int)(texture.Height * obj.scale));

                // doe voor elk object
                for (int i = 0; i < objects.Count; i++)
                {
                    PhysicsObject obj2 = objects[i];
                    // wanneer het object niet NULL EN er een plaatje op zit
                    if (PhysicsOperation.isVisual(obj2) && obj2.name != obj.name)
                    {
                        Texture2D texture2 = getAniTexture(obj2, animations);
                        if (texture2 != null)
                        {
                            Robj2 = new Rectangle((int)obj2.position.X, (int)obj2.position.Y, (int)(texture2.Width * obj2.scale), (int)(texture2.Height * obj2.scale));

                            if (collision(Robj, Robj2))
                            {
                                // hier gebeurt iets met de events
                                returnValue.Add(obj2.name);
                            }
                        }
                    }
                }
                
            }
            return returnValue;

        }


        private static Texture2D getAniTexture(PhysicsObject obj, ImageSequenceController animations)
        {
            return animations.getSequenceImage(obj.aniTexture);
        }
        
    }
}
